/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "iservice_registry.h"
#include "system_ability_definition.h"
#include "SAServiceClient.h"
#include "SAServiceProxy.h"
#include "ISAService.h"
#include "log.h"

namespace OHOS {
namespace OHMW {
DEFINE_OHMW_LOG_LABEL("OhmwSaServiceClient");

OhmwSaServiceClient::OhmwSaServiceClient() {
    sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sa_mgr == nullptr) {
        OHMW_LOGE("Failed to get SystemAbilityManager");
        return;
    }

    sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(OHMW_SA_SERVICE_ID);
    if (object == nullptr) {
        OHMW_LOGE("Failed to get connected OHMW SA SERVICE");
        return;
    }

    proxy_ = iface_cast<IOhmwSaService>(object);
    if (proxy_ == nullptr) {
        proxy_ = new (std::nothrow) OhmwSaServiceProxy(object);
    }

    if (proxy_ == nullptr) {
        OHMW_LOGE("OhmwSaServiceProxy init failed!");
    }
}

OhmwSaServiceClient::~OhmwSaServiceClient() {

}

OhmwSaServiceClient& OhmwSaServiceClient::GetInstance() {
    static OhmwSaServiceClient instance;
    return instance;
}

ErrCode OhmwSaServiceClient::LaunchOhmwServer(MWServer::Type type) {
    return proxy_->LaunchOhmwServer(type);
}
}
}
